$(function () {

    /*选择器抽取的变量*/
    var spendPoint_form = $("#spendPoint_form");
    var spendPoint_dialog = $("#spendPoint_dialog");
    var spendPoint_datagrid = $("#spendPoint_datagrid");
    var spendPointItem_form = $("#spendPointItem_form");
    var spendPointItem_dialog = $("#spendPointItem_dialog");
    var spendPointItem_datagrid = $("#spendPointItem_datagrid");


    /*字典目录数据表*/
    spendPoint_datagrid.datagrid({
        title: "支出目录",
        url: "/spendPoint/query.do",
        fit: true,
        striped: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        toolbar: "#spendPoint_toolbar",
        /*点击行事件*/
        onSelect: function (index, row) {
            //重新加载明细表
            spendPointItem_datagrid.datagrid("load",{sn:row.sn})
        },
        columns: [[
            {field: 'sn', title: '目录编号', width: 100, align: 'center'},
            {field: 'name', title: '目录名称', width: 100, align: 'center'}
        ]]
    })

    /*明细目录数据表*/
    spendPointItem_datagrid.datagrid({
        title: "支出明细",
        url: "/spendPointItem/query.do",
        fit: true,
        striped: true,
        rownumbers: true,
        sortName : 'spenddate',
        sortOrder : 'desc',
        fitColumns: true,
        singleSelect: true,
        toolbar: "#spendPointItem_toolbar",
        columns: [[
            {field: 'name', title: '明细名称', width: 180, align: 'center'},
            {field: 'spendamount', title: '支出金额', width: 180, align: 'center'},
            {field: 'spenddate', title: '支出时间', width: 180, align: 'center',sortable:'true'},
            {field: 'spenduser', title: '支出人', width: 180, align: 'center'},
            {field: 'info', title: '明细简介', width: 180, align: 'center'},
            {field: 'parent', title: '所属目录', width: 180, align: 'center',formatter:function (value,row,index) {
                    return value?value.name:'';
                }}
        ]]
    })

    /*目录编辑框*/
    spendPoint_dialog.dialog({
        width: 280,
        height: 200,
        buttons: "#spendPoint_btns",
        closed: true,
        onClose: function () {
            spendPoint_form.form("clear");
        }
    });

    /*明细编辑框*/
    spendPointItem_dialog.dialog({
        width: 280,
        height: 350,
        buttons: "#spendPointItem_btns",
        closed: true,
        onClose: function () {
            spendPointItem_form.form("clear");
        }

    });

    /*导入弹出框*/
    $("#spendPointItem_importData").dialog({
        width: 300,
        height: 115,
        closed: true,
        buttons: [{
            text: "上传",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                $("#importData_form").submit();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObject["importCancel"]();
            }
        }],
        onClose: function () {
            $("input[name='file']").prop("value", "");
        }
    })
    
    

    /*事件对象*/
    var methodObject = {
        /*导出事件*/
        export: function () {
            window.open("/spendPointItem/export.do?"+$("#spendPointItem_queyForm").serialize());
        },

        /*导入事件*/
        importData: function () {
            $("#spendPointItem_importData").dialog('open');
        },

        importCancel:function () {
            $("#spendPointItem_importData").dialog('close');
        },
        /*高级查询事件*/
        query: function () {
            alert(1);
            var beginDate = $("#beginDate").datebox('getValue');
            var endDate = $("#endDate").datebox('getValue');

            spendPointItem_datagrid.datagrid('load', {
                beginDate: beginDate,
                endDate: endDate,
            })
        },
        /*添加事件*/
        add: function () {
            spendPoint_dialog.dialog("setTitle", "添加");
            spendPoint_dialog.dialog("open");
        },
        /*编辑事件*/
        edit: function () {
            var row = spendPoint_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择一行数据', 'info');
                return;
            }
            spendPoint_dialog.dialog("setTitle", "编辑");
            /*回显*/
            spendPoint_form.form("load", row);
            spendPoint_dialog.dialog("open");
        },
        save: function () {
            spendPoint_form.form("submit", {
                url: "/spendPoint/saveOrUpdate.do",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功", "info", function () {
                            spendPoint_dialog.dialog("close");
                            spendPoint_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "error");
                    }
                }
            })
        },
        remove: function () {

            var row = spendPoint_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "info");
                return;
            }

            $.messager.confirm("温馨提示", "确定要删除吗?", function (r) {
                if (r) {
                    $.get("/spendPoint/delete.do?id=" + row.id, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","操作成功", "info", function () {
                                spendPoint_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.message,"error");
                        }
                    })
                }
            })
        },

        cancel: function () {
            spendPoint_dialog.dialog("close");
        },
        
        reload: function () {
            spendPoint_datagrid.datagrid("reload");
        },
        
        /*明细的事件,和目录一模一样*/
        addItem: function () {
            spendPointItem_dialog.dialog("setTitle", "添加");
            spendPointItem_dialog.dialog("open");
        },
        editItem: function () {

            var row = spendPointItem_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert('温馨提示', '请选择一行数据', 'info');
                return;
            }

            spendPointItem_form.form("clear");
            spendPointItem_dialog.dialog("setTitle", "编辑");


            //明细表的分类回显
            if(row.parent){
                row["parent.id"] = row.parent.id;
            }

            spendPointItem_form.form("load", row);

            spendPointItem_dialog.dialog("open");
        },
        saveItem: function () {
            spendPointItem_form.form("submit", {
                url: "/spendPointItem/saveOrUpdate.do",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功", "info", function () {
                            spendPointItem_dialog.dialog("close");
                            spendPointItem_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "error");
                    }
                }
            })
        },
        removeItem: function () {
            var row = spendPointItem_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "info");
                return;
            }

            $.messager.confirm("温馨提示", "确定要删除吗?", function (r) {
                if (r) {
                    //鑾峰彇閫変腑琛岀殑id
                    $.get("/spendPointItem/delete.do?id=" + row.id, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                spendPointItem_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.message,"error");
                        }
                    })
                }
            })
        },
        cancelItem: function () {
            spendPointItem_dialog.dialog("close");
        },
        reloadItem: function () {
            spendPointItem_datagrid.datagrid("reload");
        }
    };

    /*统一事件分配*/
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObject[cmd]();
    });
})
